using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Business.ReturnManagement.Dto;
using Business.Models;
using Microsoft.AspNetCore.Authorization;
using Business.Permissions;

namespace Business.ReturnManagement
{
    [Authorize(BusinessPermissions.Return.Default)]
    public class ReturnAppService : ApplicationService,IReturnAppService
    {
        private const string FormName = "Return";
        private IRepository<Return, Guid> _repository;

        public ReturnAppService(
            IRepository<Return, Guid> repository
            )
        {
            _repository = repository;
        }
        #region 增删改查基础方法

        public async Task<ReturnDto> Get(Guid id)
        {
            var data = await _repository.GetAsync(id);
            var dto = ObjectMapper.Map<Return, ReturnDto>(data);
            return dto;
        }

        public async Task<PagedResultDto<ReturnDto>> GetAll(GetReturnInputDto input)
        {
            var query = (await _repository.GetQueryableAsync()).WhereIf(!string.IsNullOrWhiteSpace(input.Filter), a => a.Name.Contains(input.Filter))
                .WhereIf(!string.IsNullOrWhiteSpace(input.Code), a => a.Code.Contains(input.Code))
                .WhereIf(!string.IsNullOrWhiteSpace(input.Remark), a => a.Remark.Contains(input.Remark))
                 .WhereIf(!string.IsNullOrWhiteSpace(input.Order), a => a.Remark.Contains(input.Order))
             ;
            if (input.Gong > 0)
            {
                query = query.Where(a => a.Gong==input.Gong);
            }
            if (input.State > 0)
            {
                query = query.Where(a => a.State == input.State);
            }
            if (input.Warehouse > 0)
            {
                query = query.Where(a => a.Warehouse == input.Warehouse);
            }
            if (input.Salesman > 0)
            {
                query = query.Where(a => a.Salesman == input.Salesman);
            }

            var totalCount = await query.CountAsync();
            var items = await query.OrderBy(input.Sorting ?? "Id")
                       .Skip(input.SkipCount)
                       .Take(input.MaxResultCount)
                       .ToListAsync();

            var dto = ObjectMapper.Map<List<Return>, List<ReturnDto>>(items);
            return new PagedResultDto<ReturnDto>(totalCount, dto);
        }

        public async Task<ReturnDto> DataPost(CreateOrUpdateReturnDto input)
        {
            Return result = null;
            if (!input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdateReturnDto, Return>(input));
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));
            }
            return ObjectMapper.Map<Return, ReturnDto>(result);
        }

        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }

        }

     
        #endregion

    }
}